package dao

import (
	"myapp/common"
	"myapp/model"
	"myapp/utils"

	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func OUtCar(c *gin.Context) {
	db := common.InitDB()
	var outcars []*model.Outcar
	err := db.Find(&outcars).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code: utils.SUCCESS,
			Data: gin.H{
				"outcars": outcars,
			},
		})
	}
}

func Get_Outcar(c *gin.Context) {
	db := common.InitDB()
	pagenum, _ := strconv.Atoi(c.PostForm("pagenum"))
	pagesize, _ := strconv.Atoi(c.PostForm("pagesize"))

	var outcars []*model.Outcar
	var count int64
	err := db.Order("id").Limit(pagesize).Offset((pagenum - 1) * pagesize).Find(&outcars).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	}
	err = db.Table("outcars").Count(&count).Error

	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code: utils.SUCCESS,
			Data: gin.H{
				"Outcars": outcars,
				"total":   count,
			},
		})
	}
}

func Get_id_outcar(c *gin.Context) {
	db := common.InitDB()
	id := c.Query("id")

	var outcars model.Outcar
	err := db.Where("id = ?", id).Find(&outcars).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code: utils.SUCCESS,
			Data: gin.H{
				"outcars": outcars,
			},
		})
	}
}

func Del_outcar(c *gin.Context) {
	db := common.InitDB()
	id := c.PostForm("id")

	err := db.Where("id = ?", id).Delete(&model.Outcar{}).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "删除失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code:    utils.SUCCESS,
			Message: "删除成功！",
		})
	}

}
func Add_outcar(c *gin.Context) {
	db := common.InitDB()
	outCount, _ := strconv.ParseFloat(c.PostForm("outCount"), 64)
	outWeight, _ := strconv.ParseFloat(c.PostForm("outWeight"), 64)
	extractionID := c.PostForm("extractionID")
	businessID := c.PostForm("businessID")
	sailingday, _ := time.ParseInLocation("2006-01-02", c.PostForm("sailingDay"), time.Local)
	var outcars = model.Outcar{OutCount: uint(outCount), OutWeight: outWeight, ExtractionID: extractionID,
		BusinessID: businessID, SailingDay: sailingday}

	err := db.Create(&outcars).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "创建失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code:    utils.SUCCESS,
			Message: "创建成功！",
		})
	}
}
func Modify_outcar(c *gin.Context) {
	db := common.InitDB()
	id, _ := strconv.ParseInt(c.PostForm("id"), 10, 64)

	outCount, _ := strconv.ParseFloat(c.PostForm("outCount"), 64)
	outWeight, _ := strconv.ParseFloat(c.PostForm("outWeight"), 64)
	extractionID := c.PostForm("extractionID")
	businessID := c.PostForm("businessID")

	sailingday, _ := time.ParseInLocation("2006-01-02", c.PostForm("sailingDay"), time.Local)

	var outcars = model.Outcar{ID: uint(id),
		OutCount: uint(outCount), OutWeight: outWeight, ExtractionID: extractionID, BusinessID: businessID,
		SailingDay: sailingday}
	err := db.Model(&outcars).Where("id = ?", outcars.ID).Updates(outcars).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "修改失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code:    utils.SUCCESS,
			Message: "修改成功！",
		})
	}
}
